package nl.rug.snitter.site;

import java.util.ArrayList;
import java.util.List;

import nl.rug.snitter.db.CommonDbCommands;
import nl.rug.snitter.message.ExtendedMessage;
import nl.rug.snitter.message.Message;
import nl.rug.snitter.message.SiteMessagePropagation;
import nl.rug.snitter.user.User;

public abstract class CommonSiteCommands
{

	static public void deleteDB()
	{
		CommonDbCommands.clearDb();
	}
	
	static public User getUser(String email)
	{
		return CommonDbCommands.findUserBy("email", email, true, true);
	}
	
	static public User getUser(Long id)
	{
		return CommonDbCommands.getUser(id, true, true, null);
	}
	
	static public void addUser(User user)
	{
		CommonDbCommands.AddUser(user);
	}
	
	static public Long saveMsg(User user, String msg, String title)
	{
		Message msg2= new Message(user.getId(), msg, title);
		return CommonDbCommands.saveMessage(msg2);
	}
	
	static public void addMsgToUser(User user, Long msgId)
	{	
		CommonDbCommands.AddMessageToUser(user, msgId);
	}
	
//	static public void removeMsg(User user, long msgNumber)
//	{
//		user.getMessageManager().removeMsg(msgNumber);
//		CommonDbCommands.UpdateUser(user);
//	}
	
	static public Message getMsg(Long msgId)
	{
		return CommonDbCommands.getMsg(msgId);
	}

	static public void removeMsg(User user, long msgId)
	{
		CommonDbCommands.removeMsg(msgId, user);
	}
	
	static public void removeFollowing(long idUser, long idFollwingUser)
	{
		User user= CommonDbCommands.getUser(idUser, true, true, null);
		User followingUser= CommonDbCommands.getUser(idFollwingUser, true, true, null);
		CommonDbCommands.removeFollowedBy(user, followingUser);
		CommonDbCommands.removeFollowing(followingUser, user);
	}
	
	static public ArrayList<SimpleUserInfo> search(String searchBy, String searchFor)
	{
		return CommonDbCommands.search(searchBy, searchFor);
	}
	
	static public void updateUser(User user)
	{
		CommonDbCommands.UpdateUser(user);
	}
	
	static public boolean addFollowing(long id, long idToFollow)
	{
		User user= CommonDbCommands.getUser(id, true, true, null);
		if(user.getFollowing().contains(idToFollow))
		{
			return false;
		}
		else
		{
			CommonDbCommands.AddFollowerToUser(user, idToFollow, new SiteMessagePropagation());
			return true;
		}
	}
	
	static public List<ExtendedMessage> getListOfNewMsg(int numberOfMsg, Long startId ,User user)
	{
		List<Message> tempMsgs= CommonDbCommands.getListOfNewMsg(numberOfMsg, startId, user);
		ArrayList<ExtendedMessage> msgs= new ArrayList<ExtendedMessage>();
		User tempUser= null;
		for(Message msg : tempMsgs)
		{
			tempUser= CommonDbCommands.getUser(msg.getUserId(), true, true, null);
			msgs.add(new ExtendedMessage(msg, tempUser.convertToSimpleUserInfo()));
		}
		return msgs;
	}
	
	static public List<ExtendedMessage> getPublicNewMsgList(int size)
	{
		List<Message> simpleMsgs= CommonDbCommands.getPublicListOfNewMsg(size);
		ArrayList<ExtendedMessage> extMsgs= new ArrayList<ExtendedMessage>();
		User tempUser= null;
		for(Message msg : simpleMsgs)
		{
			tempUser= CommonDbCommands.getUser(msg.getUserId(), true, true, null);
			extMsgs.add(new ExtendedMessage(msg, tempUser.convertToSimpleUserInfo()));
		}
		return extMsgs;
	}
	
	static public List<SimpleUserInfo> getFollowingOfUser(Long id)
	{
		User user= CommonDbCommands.getUser(id, true, true, null);
		ArrayList<SimpleUserInfo> following= new ArrayList<SimpleUserInfo>();
		User tempUser= null;
		for(Long tempId : user.getFollowing())
		{
			tempUser= CommonDbCommands.getUser(tempId, true, true, null);
			following.add(tempUser.convertToSimpleUserInfo());
		}
		return following;
	}
}
